package constant.milk.periodapp.setting;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import constant.milk.periodapp.R;
import constant.milk.periodapp.SuperActivity;
import constant.milk.periodapp.alarm.MilkAlarm;
import constant.milk.periodapp.dialog.MilkYesNoDialog;
import constant.milk.periodapp.util.MilkShared;
import constant.milk.periodapp.util.MilkUtil;

public class SettingActivity extends SuperActivity
{
	public static final String RESET = "RESET";
	
	private static final int REQUEST_PASSWORD = 0;
	private static final int REQUEST_SENGRI = 1;
	private static final int REQUEST_DATA = 2;
	private static final int REQUEST_COLOR= 3;
	
	private TextView backTextView;
	
	private View passwordView;
	private TextView passwordOnOffTextView;
	
	private View alarmView;
	private TextView alarmOnOffTextView;
	
	private View sengriView;
	private View colorView;
	private View dataView;
	private View resetView;
	private View emailView;
	
	private TextView versionTipTextView;
	
	private MilkYesNoDialog yesNoDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.setting_activity);
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		backTextView = (TextView) findViewById(R.id.settingBackTextView);
		
		passwordView = findViewById(R.id.settingPasswordView);
		passwordOnOffTextView = (TextView) findViewById(R.id.settingPasswordOnOffTextView);
		
		alarmView = findViewById(R.id.settingAlarmView);
		alarmOnOffTextView = (TextView) findViewById(R.id.settingAlarmOnOffTextView);
		
		sengriView = findViewById(R.id.settingSengriView);
		colorView = findViewById(R.id.settingColorView);
		dataView = findViewById(R.id.settingDataView);
		resetView = findViewById(R.id.settingResetView);
		emailView = findViewById(R.id.settingEmailView);
		
		versionTipTextView = (TextView) findViewById(R.id.settingVersionTipTextView);
	}

	@Override
	protected void setOnClick()
	{
		backTextView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		
		passwordView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				MilkShared s = new MilkShared(superContext);
				String password = s.getPassword();
				
				if (password.equals(""))
				{
					Intent intent = new Intent(superContext, PasswordActivity.class);
					intent.putExtra(PasswordActivity.PASSWORD_FLAG, PasswordActivity.SETTING);
					startActivityForResult(intent, REQUEST_PASSWORD);
				}
				else
				{
					passwordOnOffTextView.setText("OFF");
					s.setPassword("");
				}
			}
		});
		
		alarmView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				MilkShared s = new MilkShared(superContext);
				int alarm = s.getAlarm();
				
				if (alarm == 0)
				{
					alarmOnOffTextView.setText("ON");
					s.setAlarm(1);
					
					MilkAlarm ma = new MilkAlarm();
					ma.startAlarm(superContext, superDb);
				}
				else
				{
					alarmOnOffTextView.setText("OFF");
					s.setAlarm(0);
					
					MilkAlarm ma = new MilkAlarm();
					ma.stopAlarm(superContext);
				}
			}
		});
		
		sengriView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, SengriActivity.class);
				intent.putExtra(SengriActivity.SENGRI_FLAG, SengriActivity.SETTING);
				startActivityForResult(intent, REQUEST_SENGRI);
			}
		});
		
		colorView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, ColorActivity.class);
				startActivityForResult(intent, REQUEST_COLOR);
			}
		});
		
		dataView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, DataActivity.class);
				startActivityForResult(intent, REQUEST_DATA);
			}
		});
		
		resetView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				yesNoDialog = new MilkYesNoDialog(superContext, "알림", 
						getString(R.string.appname) + "을 초기화 하시겠습니까?", 
						new OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						try
						{
							MilkShared s = new MilkShared(superContext);
							
							s.removeAll();
							
							superDb.createTable(superContext);
							
							superDb.deleteTable(superContext);
							
						} catch (Exception e)
						{
							MilkUtil.showToast(superContext, "초기화에 실패 하였습니다. 개발자에게 문의 하여 주세요.");
							e.printStackTrace();
						}
						
						yesNoDialog.dismiss();
						Intent intent = new Intent();
						intent.putExtra(RESET, 1);
						setResult(Activity.RESULT_OK, intent);
						finish();
					}
				});
				
				yesNoDialog.show();
			}
		});
		
		emailView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String [] email = new String[]{getString(R.string.email)};
				
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.putExtra(Intent.EXTRA_EMAIL, email);
				intent.setType("plain/text");
				intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.appname) + " 문의사항");
				startActivity(intent);
			}
		});
	}

	@Override
	protected void setData()
	{
		MilkShared s = new MilkShared(superContext);
		String password = s.getPassword();
		
		if (password.equals(""))
		{
			passwordOnOffTextView.setText("OFF");
		}
		else
		{
			passwordOnOffTextView.setText("ON");
		}
		
		int alarm = s.getAlarm();
		
		if (alarm == 0)
		{
			alarmOnOffTextView.setText("OFF");
		}
		else
		{
			alarmOnOffTextView.setText("ON");
		}
		
		PackageInfo pi = null;
		String version = "";
		
		try
		{
			pi = getPackageManager().getPackageInfo(getPackageName(), 0);
			version = pi.versionName;
		} catch (Exception e)
		{
			e.printStackTrace();
			version = "";
		}
		
		versionTipTextView.setText(version);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_PASSWORD)
		{
			MilkShared s = new MilkShared(superContext);
			String password = s.getPassword();
			
			if (password.equals(""))
			{
				passwordOnOffTextView.setText("OFF");
			}
			else
			{
				passwordOnOffTextView.setText("ON");
			}
		}
		else if (requestCode == REQUEST_SENGRI)
		{
			Intent intent = new Intent();
			intent.putExtra(RESET, 0);
			setResult(Activity.RESULT_OK, intent);
		}
		else if (requestCode == REQUEST_DATA)
		{
			Intent intent = new Intent();
			intent.putExtra(RESET, 0);
			setResult(Activity.RESULT_OK, intent);
			finish();
		}
		else if (requestCode == REQUEST_COLOR)
		{
			Intent intent = new Intent();
			intent.putExtra(RESET, 0);
			setResult(Activity.RESULT_OK, intent);
		}
	}
}
